﻿using System;
using System.Collections.Generic;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000DC4 RID: 3524
	internal sealed class XPlayer : XRole
	{
		// Token: 0x170033C2 RID: 13250
		// (get) Token: 0x0600BFCB RID: 49099 RVA: 0x00283930 File Offset: 0x00281B30
		public XRole WatchTo
		{
			get
			{
				return (this._watch_to != null && !this._watch_to.Deprecated) ? this._watch_to : null;
			}
		}

		// Token: 0x170033C3 RID: 13251
		// (get) Token: 0x0600BFCC RID: 49100 RVA: 0x00283960 File Offset: 0x00281B60
		public XLocateTargetComponent TargetLocated
		{
			get
			{
				return this._located_target;
			}
		}

		// Token: 0x170033C4 RID: 13252
		// (get) Token: 0x0600BFCD RID: 49101 RVA: 0x00283978 File Offset: 0x00281B78
		public bool IsCorrectingMe
		{
			get
			{
				return this._correct_me;
			}
		}

		// Token: 0x170033C5 RID: 13253
		// (get) Token: 0x0600BFCE RID: 49102 RVA: 0x00283990 File Offset: 0x00281B90
		public uint[] SkillSlot
		{
			get
			{
				bool flag = base.Skill != null && base.Skill.IsSkillReplaced;
				uint[] result;
				if (flag)
				{
					result = base.Skill.ReplacedSlot;
				}
				else
				{
					result = this.Attributes.skillSlot;
				}
				return result;
			}
		}

		// Token: 0x0600BFCF RID: 49103 RVA: 0x002839D8 File Offset: 0x00281BD8
		public override bool Initilize(int flag)
		{
			base.Initilize(flag);
			this._xobject.Tag = "Player";
			this._eEntity_Type |= XEntity.EnitityType.Entity_Player;
			this._layer = XPlayer.PlayerLayer;
			this._using_cc_move = true;
			this._client_predicted = true;
			this._action = XSingleton<XInterfaceMgr>.singleton.AttachInterface<IXPlayerAction>(1U, new XPlayerAction());
			XSingleton<XInterfaceMgr>.singleton.AttachInterface<IAssociatedCamera>(XSingleton<XCommon>.singleton.XHash("IAssociatedCamera"), new XAssociatedCamera());
			bool flag2 = !XSingleton<XScene>.singleton.bSpectator;
			if (flag2)
			{
				XSingleton<XComponentMgr>.singleton.CreateComponent(this, XActionGeneratorComponent.uuID);
				bool flag3 = XSingleton<XSceneMgr>.singleton.SceneCanNavi(XSingleton<XScene>.singleton.SceneID);
				if (flag3)
				{
					this._nav = (XSingleton<XComponentMgr>.singleton.CreateComponent(this, XNavigationComponent.uuID) as XNavigationComponent);
				}
				bool flag4 = XSingleton<XSceneMgr>.singleton.CanAutoPlay(XSingleton<XScene>.singleton.SceneID) && this._xobject.IsNotEmptyObject;
				if (flag4)
				{
					bool flag5 = this._ai == null;
					if (flag5)
					{
						this._ai = (XSingleton<XComponentMgr>.singleton.CreateComponent(this, XAIComponent.uuID) as XAIComponent);
					}
					bool flag6 = this._nav == null;
					if (flag6)
					{
						this._nav = (XSingleton<XComponentMgr>.singleton.CreateComponent(this, XNavigationComponent.uuID) as XNavigationComponent);
					}
				}
			}
			bool flag7 = XSingleton<XGame>.singleton.CurrentStage.Stage != EXStage.Hall && XSingleton<XSceneMgr>.singleton.SpecifiedTargetLocatedRange(XSingleton<XScene>.singleton.SceneID) != 0f;
			if (flag7)
			{
				this._located_target = (XSingleton<XComponentMgr>.singleton.CreateComponent(this, XLocateTargetComponent.uuID) as XLocateTargetComponent);
			}
			return true;
		}

		// Token: 0x0600BFD0 RID: 49104 RVA: 0x00283B90 File Offset: 0x00281D90
		public void UpdatePlayerAttr(XPlayerAttributes attr)
		{
			this._attr = attr;
		}

		// Token: 0x0600BFD1 RID: 49105 RVA: 0x00283B9C File Offset: 0x00281D9C
		public override void OnCreated()
		{
			XSingleton<XEntityMgr>.singleton.Player = this;
			bool flag = this.Attributes.SkillLevelInfo != null;
			if (flag)
			{
				this.Attributes.SkillLevelInfo.RefreshSelfLinkedLevels(this);
			}
			base.OnCreated();
		}

		// Token: 0x0600BFD2 RID: 49106 RVA: 0x00283BE4 File Offset: 0x00281DE4
		public override void OnDestroy()
		{
			XSingleton<XEntityMgr>.singleton.Player = null;
			XSingleton<XInterfaceMgr>.singleton.DetachInterface(1U);
			XSingleton<XInterfaceMgr>.singleton.DetachInterface(XSingleton<XCommon>.singleton.XHash("IAssociatedCamera"));
			this._action = null;
			this._watch_to = null;
			base.OnDestroy();
		}

		// Token: 0x0600BFD3 RID: 49107 RVA: 0x0027D606 File Offset: 0x0027B806
		public override void Dying()
		{
			base.Dying();
		}

		// Token: 0x0600BFD4 RID: 49108 RVA: 0x00283C39 File Offset: 0x00281E39
		public override void Revive()
		{
			base.Revive();
			XSingleton<XLevelStatistics>.singleton.OnPlayerRevive();
		}

		// Token: 0x0600BFD5 RID: 49109 RVA: 0x00283C4E File Offset: 0x00281E4E
		public void PreUpdate()
		{
			this._action.RefreshPosition();
		}

		// Token: 0x0600BFD6 RID: 49110 RVA: 0x00283C60 File Offset: 0x00281E60
		public void WatchIt(XRole role)
		{
			this._watch_to = role;
			this._net.Pause = true;
			XSpectateSceneDocument specificDocument = XDocuments.GetSpecificDocument<XSpectateSceneDocument>(XSpectateSceneDocument.uuID);
			specificDocument.ChangeSpectator(role);
			XGuildArenaBattleDocument specificDocument2 = XDocuments.GetSpecificDocument<XGuildArenaBattleDocument>(XGuildArenaBattleDocument.uuID);
			specificDocument2.ChangeSpectator(role);
			this.UpdateWatcher();
			XCameraSoloComponent solo = XSingleton<XScene>.singleton.GameCamera.Solo;
			bool flag = solo == null;
			if (!flag)
			{
				solo.Stop();
				XSingleton<XScene>.singleton.GameCamera.TrySolo();
			}
		}

		// Token: 0x0600BFD7 RID: 49111 RVA: 0x00283CE0 File Offset: 0x00281EE0
		public override void OnTransform(uint to)
		{
			base.OnTransform(to);
			XBattleSkillDocument specificDocument = XDocuments.GetSpecificDocument<XBattleSkillDocument>(XBattleSkillDocument.uuID);
			bool flag = specificDocument != null;
			if (flag)
			{
				specificDocument.ResetAll(XSingleton<XScene>.singleton.SceneReady && base.IsTransform, true);
			}
		}

		// Token: 0x0600BFD8 RID: 49112 RVA: 0x00283D28 File Offset: 0x00281F28
		public override void UpdateWatcher()
		{
			bool bSpectator = XSingleton<XScene>.singleton.bSpectator;
			if (bSpectator)
			{
				bool flag = this._watch_to != null;
				if (flag)
				{
					bool deprecated = this._watch_to.Deprecated;
					if (deprecated)
					{
						this._watch_to = null;
					}
					else
					{
						base.MoveObj.Position = this._watch_to.MoveObj.Position;
						base.MoveObj.Rotation = this._watch_to.MoveObj.Rotation;
					}
				}
			}
		}

		// Token: 0x0600BFD9 RID: 49113 RVA: 0x00283DA9 File Offset: 0x00281FA9
		protected override void PositionTo(Vector3 pos)
		{
			base.PositionTo(pos);
			this._correct_me = false;
			this._action.RefreshPosition();
		}

		// Token: 0x0600BFDA RID: 49114 RVA: 0x00283DC8 File Offset: 0x00281FC8
		public override void CorrectMe(Vector3 pos, Vector3 face, bool reconnected = false, bool fade = false)
		{
			XSingleton<XActionSender>.singleton.Empty();
			bool flag = XSingleton<XScene>.singleton.bSpectator && XSingleton<XScene>.singleton.bSceneServerReady;
			if (!flag)
			{
				this._correct_me = true;
				base.CorrectMe(pos, face, reconnected, fade);
			}
		}

		// Token: 0x0600BFDB RID: 49115 RVA: 0x00283E14 File Offset: 0x00282014
		public void OnGamePause(bool pause)
		{
			if (pause)
			{
				XSingleton<XVirtualTab>.singleton.Cancel();
				base.Net.ReportMoveAction(Vector3.zero, 0.0);
				XSingleton<XActionSender>.singleton.Flush(true);
				XSingleton<XDebug>.singleton.AddLog("Player to BackGround.", null, null, null, null, null, XDebugColor.XDebug_None);
			}
		}

		// Token: 0x170033C6 RID: 13254
		// (get) Token: 0x0600BFDC RID: 49116 RVA: 0x00283E70 File Offset: 0x00282070
		public XPlayerAttributes PlayerAttributes
		{
			get
			{
				return this.Attributes as XPlayerAttributes;
			}
		}

		// Token: 0x0600BFDD RID: 49117 RVA: 0x00283E90 File Offset: 0x00282090
		protected override void Move()
		{
			bool bSpectator = XSingleton<XScene>.singleton.bSpectator;
			if (bSpectator)
			{
				this.UpdateWatcher();
			}
			else
			{
				Vector3 hitNormal = XSingleton<XScene>.singleton.GameCamera.Collision.HitNormal;
				hitNormal.y = 0f;
				Vector3 movement = this._movement;
				movement.y = 0f;
				bool flag = hitNormal.sqrMagnitude > 0f && movement.sqrMagnitude > 0f && Vector3.Angle(movement, hitNormal) > 90f;
				if (flag)
				{
					this.EdgeDetection(this.EngineObject.Forward, ref movement);
					float num = Vector3.SqrMagnitude(XSingleton<XScene>.singleton.GameCamera.CameraTrans.position - this.EngineObject.Position);
					bool flag2 = num < 1f;
					if (flag2)
					{
						this.ProjectNormal(hitNormal, ref movement);
					}
					this._movement.x = movement.x;
					this._movement.z = movement.z;
				}
				base.Move();
				this.MoveLateUpdate();
			}
		}

		// Token: 0x0600BFDE RID: 49118 RVA: 0x00283FAC File Offset: 0x002821AC
		private void MoveLateUpdate()
		{
			bool flag = XSingleton<XGame>.singleton.SyncMode && !XStateMgr.IsUnBattleState(base.Machine.Current);
			if (!flag)
			{
				List<XEntity> opponent = XSingleton<XEntityMgr>.singleton.GetOpponent(this);
				for (int i = 0; i < opponent.Count; i++)
				{
					Vector3 position = base.MoveObj.Position;
					bool isRole = opponent[i].IsRole;
					if (!isRole)
					{
						bool flag2 = (opponent[i].IsPuppet || opponent[i].IsSubstance) && !XEntity.ValideEntity(opponent[i]);
						if (!flag2)
						{
							bool flag3 = opponent[i].Skill != null && opponent[i].Skill.IsCasting() && opponent[i].Skill.CurrentSkill.MainCore.Soul.IgnoreCollision;
							if (!flag3)
							{
								XOthersAttributes xothersAttributes = opponent[i].Attributes as XOthersAttributes;
								bool flag4 = xothersAttributes == null || !xothersAttributes.Blocked;
								if (!flag4)
								{
									bool flag5 = opponent[i].MoveObj.Position.y > position.y + base.Height;
									if (!flag5)
									{
										bool flag6 = opponent[i].Present != null && opponent[i].Present.PresentLib.Huge;
										if (flag6)
										{
											SeqListRef<float> hugeMonsterColliders = opponent[i].Present.PresentLib.HugeMonsterColliders;
											for (int j = 0; j < hugeMonsterColliders.Count; j++)
											{
												float num = hugeMonsterColliders[j, 2] * opponent[i].Scale;
												this.A(position, opponent[i].HugeMonsterColliderCenter(j), base.Radius + num);
											}
										}
										else
										{
											this.A(position, opponent[i].RadiusCenter, base.Radius + opponent[i].Radius);
										}
									}
								}
							}
						}
					}
				}
			}
		}

		// Token: 0x0600BFDF RID: 49119 RVA: 0x002841E8 File Offset: 0x002823E8
		private void A(Vector3 me, Vector3 it, float r)
		{
			Vector3 v = me - it;
			v.y = 0f;
			float sqrMagnitude = v.sqrMagnitude;
			bool flag = sqrMagnitude - r * r < -0.001f;
			if (flag)
			{
				Vector3 vector = XSingleton<XCommon>.singleton.Horizontal(v);
				Vector3 vector2 = it + vector * r;
				float y = vector2.y;
				bool flag2 = XSingleton<XScene>.singleton.TryGetTerrainY(vector2, out y) && y >= 0f;
				if (flag2)
				{
					bool flag3 = XSingleton<XScene>.singleton.CheckDynamicBlock(base.MoveObj.Position, vector2);
					if (flag3)
					{
						bool standOn = base.StandOn;
						if (standOn)
						{
							vector2.y = y + 0.25f;
							base.MoveObj.Position = vector2;
							base.MoveObj.Move(Vector3.down);
						}
						else
						{
							vector2.y = me.y;
							base.MoveObj.Position = vector2;
						}
					}
				}
			}
		}

		// Token: 0x0600BFE0 RID: 49120 RVA: 0x002842EC File Offset: 0x002824EC
		private bool EdgeDetection(Vector3 forward, ref Vector3 move)
		{
			RaycastHit raycastHit;
			bool flag = this.RayDetection(forward, out raycastHit);
			bool result;
			if (flag)
			{
				this.ProjectNormal(raycastHit.normal, ref move);
				result = true;
			}
			else
			{
				result = false;
			}
			return result;
		}

		// Token: 0x0600BFE1 RID: 49121 RVA: 0x00284320 File Offset: 0x00282520
		private void ProjectNormal(Vector3 normal, ref Vector3 move)
		{
			Vector3 forward = Vector3.forward;
			Vector3 vector = normal;
			Vector3.OrthoNormalize(ref vector, ref forward);
			move = Vector3.Project(move, forward);
		}

		// Token: 0x0600BFE2 RID: 49122 RVA: 0x00284354 File Offset: 0x00282554
		private bool RayDetection(Vector3 ray, out RaycastHit hit)
		{
			int num = 513;
			Vector3 position = this.EngineObject.Position;
			position.y += base.Height;
			return Physics.Raycast(position, ray, out hit, 1f, num);
		}

		// Token: 0x0600BFE3 RID: 49123 RVA: 0x002843A4 File Offset: 0x002825A4
		public override bool CastFakeShadow()
		{
			return true;
		}

		// Token: 0x04004E65 RID: 20069
		private XLocateTargetComponent _located_target = null;

		// Token: 0x04004E66 RID: 20070
		private XRole _watch_to = null;

		// Token: 0x04004E67 RID: 20071
		private IXPlayerAction _action = null;

		// Token: 0x04004E68 RID: 20072
		private bool _correct_me = false;

		// Token: 0x04004E69 RID: 20073
		public static int PlayerLayer = LayerMask.NameToLayer("Player");
	}
}
